package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.InboundDetailMapper;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.dao.WarehouseReceiptMapper;
import com.spark.incoicings.service.WarehouseReceiptService;
import com.spark.incoicings.utils.RandomGenerateUtil;
import com.spark.incoicings.vo.WarehouseReceiptVo;
import com.spark.invoicings.entity.Code;
import com.spark.invoicings.entity.InboundDetail;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.entity.WarehouseReceipt;
import com.spark.invoicings.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class WarehouseReceiptServiceImpl
        extends ServiceImpl<WarehouseReceiptMapper, WarehouseReceipt>
        implements WarehouseReceiptService {

    @Resource
    private WarehouseReceiptMapper warehouseReceiptMapper;

    @Resource
    private InboundDetailMapper inboundDetailMapper;

    @Resource
    private RandomGenerateUtil randomGenerateUtil;

    @Resource
    private ProductMapper productMapper;

    /**
     * 根据id获取入库单
     * @param id
     * @return
     */
    @Override
    public WarehouseReceipt selectWarehouseReceiptById(Integer id) {
        WarehouseReceipt warehouseReceipt = warehouseReceiptMapper.selectWarehouseReceiptById(id);
        List<InboundDetail> inboundDetailList
                = inboundDetailMapper.selectInboundDetailById(warehouseReceipt.getReceiptId());
        warehouseReceipt.setInboundDetailList(inboundDetailList);
        return warehouseReceipt;
    }

    /**
     * 根据id删除入库单
     * @param id
     * @return
     */
    @Override
    public boolean delWarehouseReceiptById(Integer id) {
        List<InboundDetail> list = inboundDetailMapper.selectInboundDetailById(id);
        // 判断该入库单下是否有入库单明细
        if (list == null || list.isEmpty()) {
            return false;
        }
        return warehouseReceiptMapper.deleteById(id) > 0;
    }

    /**
     * 根据id更新入库单
     * @param warehouseReceipt
     * @return
     */
    @Transactional
    @Override
    public boolean updateWarehouseReceiptById(WarehouseReceipt warehouseReceipt) {
        int inboundQuantity = 0;
        for (InboundDetail inboundDetail : warehouseReceipt.getInboundDetailList()) {
            // 计算入库总量
            inboundQuantity += inboundDetail.getInboundQuantity();
            inboundDetailMapper.updateById(inboundDetail);

            Product product = productMapper.selectProductById(inboundDetail.getProductId());
            // 产品库存
            if (warehouseReceipt.getEntryStatusId() == 156 || warehouseReceipt.getEntryStatusId() == 157) {
                product.setCurrentInventory(product.getCurrentInventory() + inboundDetail.getInboundQuantity());
            } else if (warehouseReceipt.getEntryStatusId() == 158 || warehouseReceipt.getEntryStatusId() == 159){
                product.setCurrentInventory(product.getCurrentInventory() - inboundDetail.getInboundQuantity());
            }
            productMapper.updateById(product);
        }
        warehouseReceipt.setTotalQuantity(inboundQuantity);
        return warehouseReceiptMapper.updateById(warehouseReceipt) > 0;
    }

    /**
     * 查询所有的入库单
     * @param map
     * @return
     */
    @Override
    public List<WarehouseReceipt> selectWarehouseReceiptAll(Map<String, Object> map) {
        List<WarehouseReceipt> warehouseReceiptList
                = warehouseReceiptMapper.selectWarehouseReceiptAll(map);
        warehouseReceiptList.forEach(warehouseReceipt -> {
            List<InboundDetail> inboundDetailList
                    = inboundDetailMapper.selectInboundDetailById(warehouseReceipt.getReceiptId());
            warehouseReceipt.setInboundDetailList(inboundDetailList);
        });
        return warehouseReceiptMapper.selectWarehouseReceiptAll(map);
    }

    /**
     * 添加入库单
     * @param warehouseReceipt
     * @return
     */
    @Transactional
    @Override
    public Result addWarehouseReceipt(WarehouseReceipt warehouseReceipt) {
        // 生成入库单
        Code code = new Code();
        code.setType("RK");
        code.setText(code.getType() + "%s%s");
        String rkId = randomGenerateUtil.nextSerialNumber(code);
        warehouseReceipt.setReceiptNumber(rkId);
        //
        int inboundQuantity = 0;
        for (InboundDetail inboundDetail : warehouseReceipt.getInboundDetailList()) {
            // 计算入库总量
            inboundQuantity += inboundDetail.getInboundQuantity();
            inboundDetailMapper.insert(inboundDetail);
        }
        warehouseReceipt.setTotalQuantity(inboundQuantity);
        warehouseReceiptMapper.insert(warehouseReceipt);
        for (InboundDetail inboundDetail : warehouseReceipt.getInboundDetailList()) {
            inboundDetail.setInboundOrderId(warehouseReceipt.getReceiptId());
            inboundDetailMapper.updateById(inboundDetail);
        }
        return Result.success(200, "success", warehouseReceipt.getReceiptId());
    }

}
